package edu.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 数据存取
 * ServletContext#setAttribute( String key , Object value ) ;
 * ServletContext#getAttribute( String key ) ： 通过key进行获取指定的值，返回一个Object
 * getInitParameter(String s);  获取全局初始化参数值
 * getInitParameterNames(); 获取全局初始化参数名
 */
@WebServlet("/servletContext02")
public class ServletContextDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();

        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String s = initParameterNames.nextElement();
            String initParameter = servletContext.getInitParameter(s);
            System.out.println(s + "==>" + initParameter);
        }

        servletContext.setAttribute("AttrTest", "haha");
    }
}
